/**
 * 
 */
package ro.utcn.tp.taskManagement.business;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.model.User;
import ro.utcn.tp.taskManagement.persistence.PersistenceFactory;
import ro.utcn.tp.taskManagement.persistence.TasksPersistence;
import ro.utcn.tp.taskManagement.utils.DateUtils;

/**
 * @author Coni
 * 
 */
public class TasksManager {
	private static TasksManager instance;
	private TasksPersistence tasksPersistence;

	private TasksManager() {
		// private constructor
		try {
			tasksPersistence = PersistenceFactory.getInstance()
					.getTasksPersistence();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static TasksManager getInstance() {
		if (instance == null) {
			instance = new TasksManager();
		}
		return instance;
	}

	public boolean saveTask(Task task) {
		try {
			tasksPersistence.saveTask(task);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public java.util.List<Task> getUserTasks(int userId, Calendar selectedDate) {
		try {
			Calendar todayDate= DateUtils.getTodayDate();
			if(selectedDate.before(todayDate)){
				//TODO get closed tasks from selectedDate
				return tasksPersistence.getPastDayTasks(userId,selectedDate);
			}else if(selectedDate.equals(todayDate)){
				//TODO get all open tasks with schedule date less or equal today and closed tasks today
				return tasksPersistence.getTodayTasks(userId,selectedDate);
			}else {
				//TODO get tasks scheduled for selected date
				return tasksPersistence.getFutureDayTasks(userId,selectedDate);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public java.util.List<Task> getUserTasks(int userId) {
		try {
				return tasksPersistence.getUserTasks(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean deleteTask(Task task) {
		try {
			tasksPersistence.deleteTask(task);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public java.util.List<Task> searchByDescription(User user,
			String searchedTaskByDescription) {
		List<Task> foundTasks = new ArrayList();
		List<Task> tasks = getUserTasks(user.getId());
		Iterator<Task> iterator = tasks.iterator();
		while (iterator.hasNext()) {
			Task task = iterator.next();
			String[] descriptionList = task.getDescription().trim().split(" ");
			String[] searchedDescriptionList = searchedTaskByDescription.trim()
					.split(" ");
			for (int i = 0; i < descriptionList.length; i++) {
				String word = descriptionList[i];
				for (int j = 0; j < searchedDescriptionList.length; j++) {
					String searchedWord = searchedDescriptionList[j];

					if (word.contains(searchedWord)) {

						foundTasks.add(task);
					}

				}
			}

		}
		return foundTasks;
	}

	public java.util.List<Task> searchByStatus(User user,
			String searchedTaskByStatus) {
		List<Task> foundTasks = new ArrayList();
		List<Task> tasks = getUserTasks(user.getId());

		Iterator<Task> iterator = tasks.iterator();
		while (iterator.hasNext()) {
			Task task = iterator.next();
			if (task.getStatus().contains(searchedTaskByStatus)) {
				foundTasks.add(task);
			}
		}
		return foundTasks;
	}
}
